package ee.videorentalstore.bonustracker;

import java.util.HashSet;
import java.util.Set;

import ee.videorentalstore.users.types.Customer;

public class CustomerBonusPointsManager 
{
	private static CustomerBonusPointsManager instance = null;
	private Set<CustomerAndItsBonusPoints> setOfCustomersAndTheirBonusPoints = new HashSet<CustomerAndItsBonusPoints>();
	
	
	private CustomerBonusPointsManager()
	{
		
	}
	
	
	public static CustomerBonusPointsManager getInstance()
	{
		if (instance == null)
		{
			instance = new CustomerBonusPointsManager();
		}
		
		return instance;
	}
	
	
	/**
	 * 
	 * @param customerAndItsBonusPoints
	 * @return true if entry was saved for the first time, meaning that a customer 
	 * did not have a record before. False, when otherwise.
	 */
	public boolean setFirstEntryToStartTrackingCustomerAndItsBonusPoints(
			CustomerAndItsBonusPoints customerAndItsBonusPoints)
	{
		boolean entryIsMadeForTheFirstTime = checkIsEntryMadeForTheFirstTime(customerAndItsBonusPoints);
		if (entryIsMadeForTheFirstTime)
		{
			setOfCustomersAndTheirBonusPoints.add(customerAndItsBonusPoints);
			return true;
		}
		else
		{
			return false;
		}
	}
	
	
	public boolean checkWhetherCustomerBonusPointsAreBeingTracked(Customer customer)
	{
		boolean areCustomerBonusPointsBeingTracked = false;
		for (CustomerAndItsBonusPoints customerAndItsBonusPoints : setOfCustomersAndTheirBonusPoints)
		{
			if (customerAndItsBonusPoints.getCustomer().equals(customer))
			{
				areCustomerBonusPointsBeingTracked = true;
				break;
			}
		}
		return areCustomerBonusPointsBeingTracked;
	}
	
	
	public CustomerAndItsBonusPoints getCustomerAndItsBonusPoints(Customer customer)
	{
		CustomerAndItsBonusPoints customerAndItsBonusPointsToReturn = null;
		for (CustomerAndItsBonusPoints customerAndItsBonusPoints : setOfCustomersAndTheirBonusPoints)
		{
			if (customerAndItsBonusPoints.getCustomer().equals(customer))
			{
				customerAndItsBonusPointsToReturn = customerAndItsBonusPoints;
				break;
			}
		}
		return customerAndItsBonusPointsToReturn;
	}
	
	
	public CustomerAndItsBonusPoints getCustomerAndItsBonusPoints(int customerIDToLookFor)
	{
		CustomerAndItsBonusPoints customerAndItsBonusPointsToReturn = null;
		for (CustomerAndItsBonusPoints customerAndItsBonusPoints : setOfCustomersAndTheirBonusPoints)
		{
			if (customerAndItsBonusPoints.getCustomer().getUserID() == customerIDToLookFor)
			{
				customerAndItsBonusPointsToReturn = customerAndItsBonusPoints;
				break;
			}
		}
		return customerAndItsBonusPointsToReturn;
	}
	
	
	private boolean checkIsEntryMadeForTheFirstTime(
			CustomerAndItsBonusPoints customerAndItsBonusPoints)
	{
		if (setOfCustomersAndTheirBonusPoints.contains(customerAndItsBonusPoints))
		{
			return false;
		}
		else
		{
			return true;
		}
	}
}
